package com.lz.yz.empowerment.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lz.yz.base.entity.ReturnValue;
import com.lz.yz.empowerment.Param.QuestionParam;
import com.lz.yz.empowerment.entity.QuestionBuff;
import com.lz.yz.empowerment.model.QuestionDetailsModel;
import com.lz.yz.empowerment.model.QuestionModel;
import com.lz.yz.empowerment.model.ShareModel;
import com.lz.yz.empowerment.service.QuestionBuffService;
import com.lz.yz.member.entity.Member;
import com.lz.yz.member.service.AccountService;
import com.lz.yz.member.service.MemberService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* Created by CodeGenerator on 2017/11/09.
*/
@RestController
@RequestMapping("/questionBuff")
public class QuestionBuffController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private QuestionBuffService questionBuffService;

    @Autowired
    AccountService accountService;


    @Autowired
    HttpServletRequest request;


    @ApiOperation(value="发布赋能")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer "),
            @ApiImplicitParam(name = "questionModel", value = "赋能实体类", dataType = "QuestionModel", paramType = "body"),
    })
    @PostMapping("/question")
    public ReturnValue add(@RequestBody QuestionModel questionModel) {
        try {
            Integer expertAccountId= (Integer) request.getAttribute("accountId");
            questionModel.setAccountId(expertAccountId);
            return questionBuffService.add(questionModel);
        } catch (Exception e) {
            logger.error("发布赋能出错" + questionModel, e);
            return ReturnValue.generateFalseReturnValue("发布赋能失败");
        }
    }


    @ApiOperation(value="查询赋能列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer "),
            @ApiImplicitParam(paramType = "query",name = "page", value = "当前页"),
            @ApiImplicitParam(paramType = "query",name = "pageSize", value = "每页条数"),
            @ApiImplicitParam(paramType = "query",name = "typeId", value = "查询类型 1-分类ID,2-推荐,3-详情页赋能列表,4-我的賦能列表"),
            @ApiImplicitParam(paramType = "query",name = "domainId", value = "分类ID"),
            @ApiImplicitParam(paramType = "query",name = "title", value = "标题"),
            @ApiImplicitParam(paramType = "query",name = "questionId", value = "赋能ID"),
            @ApiImplicitParam(paramType = "query",name = "targetUserId", value = "關注人ID"),
    })
    @GetMapping("/questionList")
    public ReturnValue list(@RequestParam(defaultValue = "1") Integer typeId,Integer domainId,String title,Integer questionId,Integer targetUserId,
                            @RequestParam(defaultValue = "20") Integer pageSize, @RequestParam(defaultValue = "1") Integer page){
        Integer expertAccountId = accountService.getAccountIdByRequest(request);
        QuestionParam questionParam = new QuestionParam();
        questionParam.setPage(page);
        questionParam.setPageSize(pageSize);
        questionParam.setTypeId(typeId);
        questionParam.setDomainId(domainId);
        questionParam.setUserId(expertAccountId);
        questionParam.setTitle(title);
        questionParam.setTargetUserId(targetUserId);
        questionParam.setQuestionId(questionId);
        return questionBuffService.listQuestion(questionParam);
    }



    @ApiOperation(value="添加平摊记录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer "),
            @ApiImplicitParam(name = "shareModel", value = "平摊记录保存", dataType = "ShareModel", paramType = "body"),
    })
    @PostMapping("/addShare")
    public ReturnValue add(@RequestBody ShareModel shareModel) {
        try {
            Integer accountId = (Integer) request.getAttribute("accountId");
            return questionBuffService.addShareLog(accountId,shareModel);
        } catch (Exception e) {
            logger.error("添加平摊记录出错" + shareModel, e);
            return ReturnValue.generateFalseReturnValue("添加平摊记录出错");
        }
    }


    @ApiOperation(value="查询赋能详情",response = QuestionDetailsModel.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer "),
            @ApiImplicitParam(paramType = "query",name = "questionId", value = "赋能ID"),

    })
    @GetMapping("/queryQuestionInfo")
    public ReturnValue queryQuestionInfo(Integer questionId){
        Integer accountId= (Integer) request.getAttribute("accountId");
        QuestionParam questionParam=new QuestionParam();
        questionParam.setUserId(accountId);
        questionParam.setQuestionId(questionId);
        return   questionBuffService.queryQuestionDetails(questionParam);

    }


    @ApiOperation(value="查询平摊赋能详情",response = QuestionDetailsModel.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer "),
            @ApiImplicitParam(paramType = "query",name = "shareId", value = "分摊ID"),

    })
    @GetMapping("/queryShareInfo")
    public ReturnValue queryShareInfo(Integer shareId){
        Integer accountId= (Integer) request.getAttribute("accountId");
        QuestionParam questionParam=new QuestionParam();
        questionParam.setUserId(accountId);
        questionParam.setShareId(shareId);
        return   questionBuffService.queryShareInfo(questionParam);

    }


    @ApiOperation(value="通过条件查询赋能")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer "),
            @ApiImplicitParam(paramType = "query",name = "page", value = "当前页"),
            @ApiImplicitParam(paramType = "query",name = "pageSize", value = "每页条数"),
            @ApiImplicitParam(paramType = "query",name = "type", value = "1我发布的 2我购买的,3我平摊的 "),
    })
    @GetMapping("/queryMyQuestion")
    public ReturnValue list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer pageSize,Integer type) {

        try {
            PageHelper.startPage(page, pageSize);
            Integer accountId= (Integer) request.getAttribute("accountId");
            QuestionParam questionParam=new QuestionParam();
            questionParam.setUserId(accountId);
            questionParam.setUserType(type);
            return questionBuffService.queryAllQuestionList(questionParam);
        } catch (Exception e) {
            logger.error("通过条件查询赋能出错" +  e);
            return ReturnValue.generateFalseReturnValue("通过条件查询赋能出错");
        }
    }



    @ApiOperation(value="查询已赋能人员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer "),
            @ApiImplicitParam(paramType = "query",name = "page", value = "当前页"),
            @ApiImplicitParam(paramType = "query",name = "pageSize", value = "每页条数"),
            @ApiImplicitParam(paramType = "query",name = "questionId", value = "赋能ID"),
    })
    @GetMapping("/questionMemberList")
    public ReturnValue questionMemberList(Integer questionId,
                            @RequestParam(defaultValue = "20") Integer pageSize, @RequestParam(defaultValue = "1") Integer page){
        PageHelper.startPage(page, pageSize);
        Integer expertAccountId= (Integer) request.getAttribute("accountId");
        QuestionParam questionParam=new QuestionParam();
        questionParam.setUserId(expertAccountId);
        questionParam.setQuestionId(questionId);
        return   questionBuffService.querTeachMember(questionParam);

    }


    @ApiOperation(value="同步赋能")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer "),
            @ApiImplicitParam(name = "questionModel", value = "赋能实体类", dataType = "QuestionModel", paramType = "body"),
    })
    @PostMapping("/synchronizeQuestion")
    public ReturnValue synchronizeQuestion(@RequestBody QuestionModel questionModel) {
        try {
            Integer expertAccountId= (Integer) request.getAttribute("accountId");
            questionModel.setAccountId(expertAccountId);
            return questionBuffService.addUnanimousQuestion(questionModel);
        } catch (Exception e) {
            logger.error("同步赋能异常" + questionModel, e);
            return ReturnValue.generateFalseReturnValue("同步赋能异常");
        }
    }



    @ApiOperation(value="查询我同步的赋能列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer "),
            @ApiImplicitParam(paramType = "query",name = "page", value = "当前页"),
            @ApiImplicitParam(paramType = "query",name = "pageSize", value = "每页条数"),
    })
    @GetMapping("/queryMyTbQuestionList")
    public ReturnValue queryMyTbQuestionList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            PageHelper.startPage(page, pageSize);
            Integer accountId= (Integer) request.getAttribute("accountId");
            QuestionParam questionParam=new QuestionParam();
            questionParam.setUserId(accountId);
            return questionBuffService.unanimousQuestionList(questionParam);
        } catch (Exception e) {
            logger.error("查询我同步的赋能列表出错" +  e);
            return ReturnValue.generateFalseReturnValue("查询我同步的赋能列表出错");
        }
    }


    @ApiOperation(value="查询我同步的赋能详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token",defaultValue = "Bearer "),
            @ApiImplicitParam(paramType = "query",name = "questionTbId", value = "同步的赋能ID"),
    })
    @GetMapping("/queryMyTbQuestion")
    public ReturnValue queryMyTbQuestion(String questionTbId) {
        try {
            Integer accountId= (Integer) request.getAttribute("accountId");
            QuestionParam questionParam=new QuestionParam();
            questionParam.setUserId(accountId);
            questionParam.setQuestionTbId(questionTbId);
            return questionBuffService.queryMyTbQuestion(questionParam);
        } catch (Exception e) {
            logger.error("查询我同步的赋能详情出错" +  e);
            return ReturnValue.generateFalseReturnValue("查询我同步的赋能详情出错");
        }
    }






}
